package com.bee.bootstrap.formDesign.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.bee.bootstrap.formDesign.entity.FormRecord;
import com.bee.bootstrap.formDesign.entity.FormTemplate;
import com.bee.bootstrap.formDesign.service.IFormRecordService;
import com.bee.bootstrap.formDesign.service.IFormTemplateService;
import com.bee.bootstrap.system.service.IDictionaryService;
import com.bee.common.core.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/formtemplates")
@Api(value = "/formtemplates", description = "表单设计")
public class FormTemplateController {

    @Autowired
    private IDictionaryService dictionaryServiceImpl;
    @Autowired
    private IFormTemplateService formTemplateServiceImpl;
    @Autowired
    private IFormRecordService formRecordServiceImpl;


    @GetMapping("/dictionary")
    @ApiOperation(value="获取表单设计器的数据字典", notes="获取表单设计器的数据字典")
    public Result findDictionaryType(){
        return dictionaryServiceImpl.selectFormDictionary();
    }

    @GetMapping("/dictionary/form/{id}")
    @ApiOperation(value="获取类型为form且对应主键下的数据字典", notes="获取类型为form且对应主键下的数据字典")
    public Result findDictionaryForm(@PathVariable(name = "id") Long parentId){
        return dictionaryServiceImpl.selectFormTypeDictionarySub(parentId);
    }

    @GetMapping("/dictionary/interface/{id}")
    @ApiOperation(value="获取类型为interface且对应主键下的数据字典", notes="获取类型为interface且对应主键下的数据字典")
    public Result findInterfaceResource(@PathVariable(name = "id") Long id){
        return dictionaryServiceImpl.findInterfaceResource(id);
    }



    @GetMapping("/translate")
    @ApiOperation(value="请求第三方翻译", notes="请求第三方翻译")
    public Result findTranslate(@RequestParam("text") String text){
        return dictionaryServiceImpl.findTranslate(text);
    }

    @PostMapping("")
    @ApiOperation(value="保存表单模板", notes="保存表单模板")
    public Result addFormTemplate(@Valid @RequestBody FormTemplate formTemplate, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Result.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return formTemplateServiceImpl.addTemplateForm(formTemplate);
    }

    @PutMapping("/{formTemplateId}")
    @ApiOperation(value="更新表单模板", notes="更新表单模板")
    public Result updateFormTemplate(@Valid @RequestBody FormTemplate formTemplate, BindingResult bindingResult,@PathVariable(name ="formTemplateId") Long formTemplateId){
        if(bindingResult.hasErrors()){
            return Result.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return formTemplateServiceImpl.updateTemplateForm(formTemplate,formTemplateId);
    }

    @GetMapping("")
    @ApiOperation(value="获取表单分页对象", notes="获取表单分页对象")
    public Result findFormDesigns(@ModelAttribute FormTemplate formTemplate, Page<FormTemplate> page){
        return formTemplateServiceImpl.findPageView(page,formTemplate);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除表单")
    public Result deleteFormDesign(@PathVariable(required = true,value ="id") long id){
        return formTemplateServiceImpl.deleteFormTemplateById(id);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "获取表单模板对象")
    public Result findFormTemplate(@PathVariable(required = true,value ="id") long id){
        return formTemplateServiceImpl.findFormTemplate(id);
    }

    @PostMapping("/{templateId}/record")
    @ApiOperation(value="表单记录存储", notes="表单记录存储")
    public Result addFormDesign(@PathVariable(name = "templateId") String templateId,@RequestBody FormRecord formRecord){
        return formRecordServiceImpl.addFormRecord(formRecord);
    }

    @GetMapping("/{templateId}/record/{formRecordId}")
    @ApiOperation(value="获取表单模板下的表单数据记录", notes="获取表单模板下的表单数据记录")
    public Result findOneFormRecordData(@PathVariable(name = "templateId") Long templateId,@PathVariable(name = "formRecordId") Long id){
        return formRecordServiceImpl.findOneFormRecordData(id);
    }

    @GetMapping("/{templateId}/record")
    @ApiOperation(value="获取表单模板下的表单数据记录", notes="获取表单模板下的表单数据记录")
    public Result findFormRecordAndData(@RequestParam(required = false,name = "formRecordQuery") String formRecordQuery, @PathVariable(name = "templateId") Long templateId, Page<FormRecord> page){
        return formRecordServiceImpl.findFormRecordAndData(templateId,page,formRecordQuery);
    }

    @DeleteMapping("/{templateId}/record/{id}")
    @ApiOperation(value="删除表单记录", notes="删除表单记录")
    public Result findFormRecordAndData(@PathVariable(name = "templateId") Long templateId,@PathVariable(name = "id") Long id){
        return formRecordServiceImpl.deleteFormRecordCascade(id);
    }

    @PutMapping("/{templateId}/record/{id}")
    @ApiOperation(value="更新表单记录", notes="更新表单记录")
    public Result updateFormReocrdAndData(@RequestBody FormRecord formRecord,@PathVariable(name = "templateId") Long templateId,@PathVariable(name = "id") Long id){
        return formRecordServiceImpl.updateFormReocrdAndData(formRecord);
    }


}
